package com.gskj.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gskj.eduservice.entity.EduChapter;
import com.gskj.eduservice.entity.EduVideo;
import com.gskj.eduservice.entity.chapter.ChapterVO;
import com.gskj.eduservice.entity.chapter.VideoVO;
import com.gskj.eduservice.mapper.EduChapterMapper;
import com.gskj.eduservice.service.IEduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gskj.eduservice.service.IEduVideoService;
import com.gskj.servicebase.exceptionhandler.GuLiException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-16
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements IEduChapterService {

    @Autowired
    private IEduVideoService iEduVideoService;

    @Override
    public List<ChapterVO> getChapterVideoByCourseId(String courseId) {

        // 1.根据课程id 查询章节
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<EduChapter> chapterList = baseMapper.selectList(wrapper);

        // 2.根据课程id 查询小结
        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<EduVideo> videoList = iEduVideoService.list(wrapper2);

        // 创建集合 存放 章节
        List<ChapterVO> chapterVOList = new ArrayList<>();

        // 3.封装 章节
        // 遍历当前查询的 章节
        for (EduChapter chapter : chapterList) {
            // 创建 视图对象 ChapterVO 封装 章节部分信息
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(chapter, chapterVO);
            // 存放到 chapterVOList
            chapterVOList.add(chapterVO);

            // 4.封装小结
            // 创建集合 保存 小结，并赋值给当前章节的 videoVOList
            List<VideoVO> videoVOList = new ArrayList<>();
            // 遍历 查询发 小结集合
            for (EduVideo video : videoList) {
                // 判断当前 小结的 章节id是否跟当前遍历的 章节id一致
                if (chapter.getId().equals(video.getChapterId())) {
                    // 说明当前小结属于当前章节
                    VideoVO videoVO = new VideoVO();
                    BeanUtils.copyProperties(video, videoVO);
                    // 存放
                    videoVOList.add(videoVO);
                }
            }
            // 将筛选的 videoVOList 赋值给 当前的 chapterVO
            chapterVO.setVideoVOList(videoVOList);
        }
        return chapterVOList;
    }

    @Override
    public boolean deleteChapter(String id) {

        // 1.查询该章节下有没有 小结，有提示不能删除。
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", id);
        int count = iEduVideoService.count(wrapper);

        if (count > 0) {
            // 说明该章节下还有小结,抛出一次
            throw new GuLiException(20001, "该章节下还有小结，不能直接删除，需将该所属下的小结删除");
        } else {
            // 删除章节
            int i = baseMapper.deleteById(id);

            return i > 0;
        }
    }
}
